(function($) {
$.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
   };
 })(jQuery);
(function($) {
$.fn.retcenter = function (width,height) {
   // this.css("position","absolute");
   var top = ( $(window).height() - height ) / 2+$(window).scrollTop();
   var left= ( $(window).width() - width ) / 2+$(window).scrollLeft();
    return {left:left,top:top};
   };
 })(jQuery);
function return_center(width,height) {
	var top = ( $(window).height() - height ) / 2+$(window).scrollTop();
	var left= ( $(window).width() - width ) / 2+$(window).scrollLeft();
return {left:left,top:top};
}
(function($) {
   $.fn.dialogue = function(settings) {
	/*
	var br=0;

	this.resize=function() {
		
		this.br=1;
		console.log('Got to this bit');
	
	return true;
	}
	if(this.br==1) {
	//return this;	
	alert('I should be breaking out of the function');
	return this;
	}
	*/
	var totalHeight=$(document).height();

	var config = 
		{
			width:400,
			height:400,
			cls:'dialogue',
			content:'&nbsp;<img src="/images/loading.gif" style="vertical-align:middle;" /> Loading Please Wait',
			heading:'<h5>My heading</h5>',
			closeclass:'dialogue-close',
			isAjax:false,
			ajaxUrl:null,
			removeOverrlay:true,
			removeDialogue:true,
			addOverlay:true,
			useRand:true,
			closeOnOverlayClick:true,
			canDrag:false,
			contain:false,
			animate:true,
			_this:null
		};
	//alert('Creating Dialogue');
	if (settings) $.extend(config, settings);
	//alert(config.heading);
/*
if(config.removeOverrlay) {
	$("#overlay").remove();
}
if(config.removeDialogue) {
	$("#dialogue").remove();
}
*/

var rand=(config.useRand) ? new Date().getTime() : '';

if(config.addOverlay) {
	
	$("<div>").attr("id","overlay"+rand+"").css({height:(totalHeight)+'px'}).prependTo("body").addClass('overlayz'); 
	
	
	//$("#overlay").show();		

}

/*
Build it then add it to the dom?
Then work out if it needs animating or not

*/

/* ANIMATION ? */
var dch=(config.height-30);
$("<div>").attr("id","dialogue"+rand+"").addClass(config.cls).appendTo("body").css({width:'0px',height:'0px','zIndex':'15'});//,'display':'none'
$("<div>").attr("id","dialogue-heading"+rand+"").addClass("dialogue-heading").html(config.heading).appendTo("#dialogue"+rand+"");
$("<div>").attr("id","dialogue-close"+rand+"").addClass(config.closeclass).addClass("tip").appendTo("#dialogue-heading"+rand+"").attr("title","Click to close");
$("<div>").attr("id","dialogue-content"+rand+"").addClass('dialogue-content').html(config.content).appendTo("#dialogue"+rand+"").height(dch);

/* Now it's added to the dom properly */
$dialogue		=	$("#dialogue"+rand);
$heading		=	$("#dialogue-heading"+rand);
$close			=	$("#dialogue-close"+rand);
$content		=	$("#dialogue-content"+rand);
$cent			=	return_center(config.width,config.height);
if(config.animate) {
	var c=config._this; // this should be a jquery object
	var co=c.offset();
	//$("<div>").attr("id","dialogue"+rand+"").addClass(config.cls).appendTo("body").css({width:'0px',height:'0px','zIndex':'15',left:co.left+'px'}).animate();
	// get the center places too
	//console.log(c);
	$dialogue.css({left:co.left+'px',top:co.top+'px'}).animate({
		left:$cent.left+'px',
		top:$cent.top+'px',
		width:config.width+'px',
		height:config.height+'px'
	},300,function(){
	// ajax goes here and $(this); is the current element ($dialogue)
	$.post(config.ajaxUrl,{rand:rand},function(data){
	$content.html(data);
});	
	
	
	});
} else {
	$dialogue.css();
//$("<div>").attr("id","dialogue"+rand+"").css({height:config.height+'px',width:config.width+'px','zIndex':'15',display:'none'}).addClass(config.cls).appendTo("body").center().slideDown("fast");
}

//var sbdims=return_center(config.width,config.height);
	
//$("<div>").attr("id","dialogue"+rand+"").css({height:config.height+'px',width:config.width+'px','zIndex':'15',display:'none',top:'-'+(config.height+5)+'px','left':sbdims.left+'px'}).addClass(config.cls).appendTo("body").animate({'top':sbdims.top+'px'},500,function(){$("#dialogue"+rand).slideDown()});	

//var header='<div class="dialogue-heading">'+config.heading+'</div>';
//$("<div>").attr("id","dialogue-heading"+rand+"").addClass("dialogue-heading").html(config.heading).appendTo("#dialogue"+rand+"");
//$("<div>").attr("id","dialogue-close"+rand+"").addClass(config.closeclass).addClass("tip").appendTo("#dialogue-heading"+rand+"").attr("title","Click to close");
/*
var dch=(config.height-30);
$("<div>").attr("id","dialogue-content"+rand+"").addClass('dialogue-content').html(config.content).appendTo("#dialogue"+rand+"").height(dch);
*/


if(config.isAjax) {
$.post(config.ajaxUrl,{rand:rand},function(data){
	$("#dialogue-content"+rand+"").html(data);
});	
	
}

// if it's draggable
if(config.canDrag) {
	$("#dialogue"+rand).drag({contain:config.contain});
		
}
//if(config.closeOnOverlayClick) {
$("#dialogue-close"+rand+", #overlay"+rand+"").click(function(){
	$("#tooltip").hide();
	
	
	if(config.animate) {
		$dialogue.animate({width:'0px',height:'0px',display:'none',left:co.left+'px',top:co.top+'px'},300,function(){
		$(this).empty().remove();
		if(config.useRand) {
		$("#overlay"+rand+"").remove();
		} else {
		$("#overlay").hide();	
		}
		});
	} else {
	$dialogue.fadeOut(300,function(){
		
		
		$(this).empty().remove();
		
		
		
		if(config.useRand) {
		$("#overlay"+rand+"").remove();
		} else {
		$("#overlay").hide();	
		}
	});	
	}
});


return this;
   };
 })(jQuery);
